package org.apache.oltu.oauth2.client.demo;


import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.annotate.JsonMethod;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.type.TypeFactory;

/**
 * 
 * @title: VOUtil
 * @description: json转vo , vo 转JSON
 * @company: gdyd
 * @className: VOUtil.java
 * @author: fengzt
 * @createDate: 2014年6月4日
 * @updateUser: fengzt
 * @version: 1.0
 */
public class JsonBeanUtil {

    private static Logger log = Logger.getLogger( JsonBeanUtil.class );
    
    /**
     * 
     * @description:vo转json
     * @author: fengzt
     * @createDate: 2014年6月4日
     * @param vo
     * @return:json
     */
    public static String fromVoToJsonUtil(Object vo) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility( JsonMethod.FIELD, org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.ANY );
        mapper.configure( SerializationConfig.Feature.INDENT_OUTPUT, true );
        String json = null;
        try {
            // 页面VO 转JSON
            json = mapper.writeValueAsString( vo );
            //json = URLEncoder.encode( json, "utf-8" );
        } catch (JsonGenerationException e) {
            log.error( e.getMessage(), e );
        } catch (JsonMappingException e) {
            log.error( e.getMessage(), e );
        } catch (IOException e) {
            log.error( e.getMessage(), e );
        }
        return json;
    }
    
    /**
     * 
     * @description:json转vo
     * @author: fengzt
     * @createDate: 2014年6月4日
     * @param dataJson
     * @param clss
     * @return:
     */
    public static <T> T fromJsonToVoUtil(String dataJson , Class<T> clss ) {
        ObjectMapper objectMapper = new ObjectMapper();
        T vo = null;
        try {
            // 1.获取页面参数，解决乱码，tomcat默认是按iso-8859-1的编码传输
            dataJson = URLDecoder.decode( dataJson, "utf-8" );
           // dataJson = new String( dataJson.getBytes( "ISO-8859-1" ), "utf-8" );
            vo  = objectMapper.readValue( dataJson, clss );
        } catch (JsonParseException e) {
            log.error( e.getMessage(), e );
        } catch (JsonMappingException e) {
            log.error( e.getMessage(), e );
        } catch (IOException e) {
            log.error( e.getMessage(), e );
        }
        return vo;
    }
   
    /**
     * 
     * @description:json 转 HashMap
     * @author: fengzt
     * @createDate: 2014年6月4日
     * @param dataJson
     * @return:
     */
    public static HashMap<String, Object> fromJsonToHashMap( String dataJson ){
        ObjectMapper objectMapper = new ObjectMapper();
        HashMap<String, Object> vo = new HashMap<String, Object>();
        try {
            // 1.获取页面参数，解决乱码，tomcat默认是按iso-8859-1的编码传输
            dataJson = URLDecoder.decode( dataJson, "utf-8" );
            vo  = objectMapper.readValue( dataJson, HashMap.class );
        } catch (JsonParseException e) {
            log.error( e.getMessage(), e );
        } catch (JsonMappingException e) {
            log.error( e.getMessage(), e );
        } catch (IOException e) {
            log.error( e.getMessage(), e );
        }
        return vo;
    }
    
    /**
     * 
     * @description:json转List<Object>
     * @author: fengzt
     * @param <T>
     * @createDate: 2014年6月4日
     * @param dataJson
     * @param clss
     * @return:List<T>
     */
    public static  <T> List<T> fromJsonToListObject(String dataJson , Class<T> clazz ) {
        ObjectMapper objectMapper = new ObjectMapper();
        List<T> vos = new ArrayList<T>();
        try {
            // 1.获取页面参数，解决乱码，tomcat默认是按iso-8859-1的编码传输
            dataJson = URLDecoder.decode( dataJson, "utf-8" );
           // dataJson = new String( dataJson.getBytes( "ISO-8859-1" ), "utf-8" );
             vos  = objectMapper.readValue( dataJson,
                    TypeFactory.defaultInstance().constructCollectionType(List.class,  
                            clazz) );
        } catch (JsonParseException e) {
            log.error( e.getMessage(), e );
        } catch (JsonMappingException e) {
            log.error( e.getMessage(), e );
        } catch (IOException e) {
            log.error( e.getMessage(), e );
        }
        return vos;
    }
    
    public static void main(String[] args) {
      /* String json = "[{'category':'itc_le_category_2','startDate':'2014-09-01 09:45','endDate':'2014-09-17 10:50','leaveDays':'1'},{'category':'itc_le_category_2','startDate':'2014-09-09 09:45','endDate':'2014-09-17 13:45','leaveDays':'2.3'}]";
       JSONObject jsonObj = JSONObject.fromObject(json);
       System.out.println( jsonObj);
        List<LeaveItemVo> result = VOUtil.fromJsonToListObject( json, LeaveItemVo.class );
        
        System.out.println( result.get( 0 ) );*/
        
    }
    
}
